import {createRouter,createWebHashHistory} from 'vue-router'
import store  from '@/store'

// 常量路由 , 动态路由(异步)特点 

import routerStatic from './routerStatic.js'



//异步路由

import routerAsync from './routerAsync.js'
const router = createRouter({
    history:createWebHashHistory(),
    routes:[...routerStatic,...routerAsync]
}) 
const whiteRoute =[]

router.beforeEach((to,from,next)=> {
//使用路由前置路由,将常量路径里面的name整理成一个数组 ['login','404']
    //console.log(to,from);
   if(store.getters["user/getRole"] == 2){
    next();
   } 
   const getNames = routerStatic => routerStatic.flatMap(item => [item.name, ...(Array.isArray(item.children) ? getNames(item.children) : [])]).filter(item => item != undefined)
	const names = getNames(routerStatic);


    //如果你要去的页面不在百名单上的话就需要带上token
    const arr = routerStatic.map(item=>item.name)
   // console.log(arr);
    if(!names.includes(to.name)){
        //带上token
        const token = store.getters["user/getToken"]
        if(!token){
            next("/login")
        }
        //如果你没有这个权限就拦截
        const checkedkeys = store.getters["user/getcheckedkeys"]
        if(!checkedkeys.includes(to.name)){
            //去往403页面
            next({ name : 'exception_page403'})
        }

    }
 next() 
})







export default router;